package com.hsg.common.thirdloginutil.login.line

import android.content.Intent
import android.util.Log
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment
import com.hsg.common.thirdloginutil.bean.ThirdLoginRespond
import com.hsg.common.thirdloginutil.login.LoginInterface
import com.linecorp.linesdk.LineApiResponseCode
import com.linecorp.linesdk.Scope
import com.linecorp.linesdk.auth.LineAuthenticationParams
import com.linecorp.linesdk.auth.LineLoginApi
import java.util.*

/**
 * line登入
 */
class LineLogin(
//    private val activity: AppCompatActivity,
    private val fragment: Fragment,
    private val lineChannelId: String,
    private val loginCallback: (respond: ThirdLoginRespond?) -> Unit
) : LoginInterface {

//    private val LOGIN_REQUEST_CODE_LINE = 1101

    private var registerForActivityResult: ActivityResultLauncher<Intent>? = null

    init {
        registerForActivityResult = fragment.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { activityResult ->
            handleLoginResult(0, 0, activityResult.data)
        }
    }

    override fun startLogin() {
        try {
            val loginIntent = LineLoginApi.getLoginIntent(
                fragment.requireContext(),
                lineChannelId,
                LineAuthenticationParams.Builder()
                    .scopes(Arrays.asList(Scope.PROFILE))
                    .botPrompt(LineAuthenticationParams.BotPrompt.normal)
                    .build()
            )
//            activity.startActivityForResult(loginIntent, LOGIN_REQUEST_CODE_LINE)
            registerForActivityResult?.launch(loginIntent)
        } catch (e: Exception) {
            Log.e("ERROR", e.toString())
            loginCallback(null)
        }
    }

    override fun handleLoginResult(requestCode: Int, resultCode: Int, data: Intent?) {
//        when (requestCode) {
//            LOGIN_REQUEST_CODE_LINE -> {
                val loginResult = LineLoginApi.getLoginResultFromIntent(data)
                when (loginResult.responseCode) {
                    LineApiResponseCode.SUCCESS -> {
                        val respond = ThirdLoginRespond(
                            loginResult.lineProfile!!.userId,
                            loginResult.lineCredential!!.accessToken.tokenString,
                            loginResult.lineProfile!!.displayName,
                            loginResult.lineProfile!!.pictureUrl?.toString()
                        )
                        loginCallback(respond)
                    }
                    LineApiResponseCode.CANCEL -> {
                        loginCallback(null)
                        Log.e("ERROR", "LINE Login Canceled by user!!")
                    }
                    else -> {
                        Log.e("ERROR", "Login FAILED!")
                        Log.e("ERROR", loginResult.errorData.toString())
                        loginCallback(null)
                    }
                }
//            }
//        }
    }

    override fun signOut() { //Line没有登出功能

    }

}